package com.tantinevincent.main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.tantinevincent.formatter.CsvFormatter;
import com.tantinevincent.formatter.IFormatter;
import com.tantinevincent.option.Option;
import com.tantinevincent.parser.CsvParser;
import com.tantinevincent.parser.IParser;
import com.tantinevincent.parser.LibsvmParser;
import com.tantinevincent.process.ParsingProcess;


public class Main {
	
	private static List<IParser> parsers = makeParsers();
	private static List<IFormatter> formatters = makeFormatters();
	
	private int parserIndex = -1;
	private int formatIndex = -1;
	
	private static final int SOURCE_FILE_INDEX = 0;
	private static final int SOURCE_FORMAT_NUM_INDEX = 1;
	private static final int SAVING_FORMAT_NUM_INDEX = 2;
	private static final int SAVING_FILE_INDEX = 3;
	
	public static void main(String[] args) {
		if( args.length < 4 ){
			exitWithPrintHelp();
			return;
		}
		checkSourceFile(args[SOURCE_FILE_INDEX]);
		checkCommandNum(args[SOURCE_FORMAT_NUM_INDEX],parsers.size(),"sourceformatNum"); // check source format number is correct or not
		checkCommandNum(args[SAVING_FORMAT_NUM_INDEX],formatters.size(),"savingFormatNum"); // check saving format number is correct or not
		checkSavingFile(args[SAVING_FILE_INDEX]);
		Option option = new Option();
		for( int i=4; i<=args.length-1; i++ ){
			option.load(args[i]);
		}
		int parserIndex = parserInt(args[SOURCE_FORMAT_NUM_INDEX]);
		IParser parser = parsers.get(parserIndex);
		ParsingProcess parsingProcess = new ParsingProcess(parser);
		
		//List<LabeledVector> vectors 
		
		
	}
	private static void exitWithPrintHelp(){
		System.out.println("Usage: sourceFile sourceformatNum savingFormatNum savingPath [options]");
		System.out.println("  Source Format number: ");
		printList(parsers);
		System.out.println("  Saving Format number: ");
		printList(formatters);
		System.out.println("  Options: ");
		Option.printHint();
	}
	private static void checkSourceFile(String path){
		File f = new File(path);
		if( !f.exists() || !f.canRead() ){
			System.err.println("Error in sourceFile: file is not exist or can't be read.");
			System.exit(-1);
		}
	}
	private static void checkSavingFile(String path){
		File f = new File(path);
		try {
			if( !f.exists() ){
				f.createNewFile();
			}
			if( !f.canWrite() ){
				throw new IOException();
			}
		} catch (IOException e) {
			f.delete();
			System.err.println("Error in savingFile: object path can't create new file.");
			System.exit(-1);
		}
	}	
	private static void checkCommandNum(String input,int size,String partName){
		try{
			int num = Integer.parseInt(input);
			if( num >= parsers.size() ){
				System.err.println("Error in " + partName + ": input num is out of range.");
			}
		}catch( Exception ex ){
			System.err.println("Error in " + partName + ": input num is not correct.");
			System.exit(-1);
		}
	}
	private static List<IParser> makeParsers(){
		List<IParser> parsers = new ArrayList<IParser>();
		parsers.add( new LibsvmParser() );
		parsers.add( new CsvParser() );
		return parsers;
	}
	private static List<IFormatter> makeFormatters(){
		List<IFormatter> formatters = new ArrayList<IFormatter>();
		formatters.add( new CsvFormatter() );
		return formatters;
	}
	private static void printList(List<? extends Object> list){
		for( int i=0; i<=list.size()-1; i++ ){
			System.out.println("     " + i + " : " + list.get(i).toString() );
		}
	}
	private static int parserInt(String s){
		return Integer.parseInt(s);
	}
}
